home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 158_01 / qe2attk < prev    next >
Text File  |  1987-10-10  |  10KB  |  461 lines

  1. /*  VERSION 0001  (DATE: 24/10/86)  (TIME: 21:16)  */
  2. /*
  3.     e screen editor
  4.  
  5.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  6.  
  7.     August-December 1981
  8.  
  9.         Modified:  October 1984 -- J.W. Haefner   Logan, UT (USA)
  10.                    Change "crdelete", "scroll", "crinsert"
  11.                    to work with Adds Viewpoint 3A+
  12.                    Add: "adputpage" to rewrite screen from line on down
  13.     
  14.     FILE: qe2a(dds)
  15.  
  16.     FUNCTIONS: movechar,moveline, maovepage, moveword, insertchar,firstwhite,
  17.             deletechar,crdelete,deleteword,crinsert,adjustc,
  18.             sync.
  19.  
  20.     PURPOSE: perform text changing commands
  21.  
  22. */
  23.  
  24. #include "qe.h"
  25. #define LF 0x0a
  26.  
  27. movechar(move)    /*move cursor by 'move' columns to the right
  28.           return YES unless going off text*/
  29. int move;
  30. {
  31.     int cp, len, result;
  32.  
  33.     cp=charn+move;
  34.     result=YES;
  35.     if (cp < 0) {
  36.         result=moveline(-1); 
  37.         if (result) {
  38.             cursorx=adjustc(LLIM); 
  39.             movechar(cp+1);
  40.             }
  41.         else sync(0);
  42.         }
  43.     else if (cp > (len=strlen(text))) {
  44.         result=moveline(1); 
  45.         if (result) {
  46.             sync(0);
  47.             movechar(cp-len-1);            
  48.             }
  49.         else cursorx=adjustc(LLIM);
  50.         }
  51.     else sync(cp);
  52.     return result;
  53. }
  54.  
  55. moveline(move)    /*move cursor by 'move' lines down 
  56.          return YES if Ok, NO if going off text*/
  57. int move;
  58. {
  59.     int line;
  60.  
  61.     puttext();
  62.     if ( (move<0?-move:move) > SHEIGHT) gotoxy(WAITPOS,0);
  63.     line=cline;
  64.     if ((cline=loc(cline,move)) == line) return NO;
  65.     gettext(cline);
  66.     if (cline < pfirst || cline > plast) {
  67.         if (move == 1 || move == -1) scroll(move);
  68.         else putpage();
  69.         }
  70.     else {
  71.         if ( (altered) || (offset) || (blocking)) putline(line,cursory);
  72.         cursory+=cline-line;
  73.         cursorx=adjustc(cursorx);
  74.         if ( (!altered) && (!offset) && (!blocking)) gotoxy(cursorx,cursory);
  75.         else putline(cline,cursory);
  76.         }
  77.     return YES;
  78. }
  79.  
  80. scroll(move)    /*scroll up (move==1) or down 1 line*/
  81. int move;
  82. {
  83.     int y,line;
  84.     if (move == 1) {
  85. /*        linedelete(topline);  NOT for Adds Viewpoint    */
  86.         if (topline == 1) {
  87.             gotoxy(0,SHEIGHT+1);
  88.             putch(LF);
  89.             deleteline(0,0);
  90.             gotoxy(cursorx,cursory);
  91.             putline(cline-1,SHEIGHT-1);
  92.             cursorx=adjustc(cursorx);
  93.             putline(cline,cursory);
  94.             if (plast-pfirst == (SHEIGHT-topline)) {
  95.                 plast+=move;
  96.                 }
  97.             pfirst+=move;
  98.             }
  99.         else {
  100. /*            delpage(topline);*/
  101. /*            pfirst = loc(pfirst,topline);*/
  102. /*            plast = loc(pfirst,SHEIGHT-topline);*/
  103.             pfirst++;
  104.             plast++;
  105.             adputpage(pfirst,topline);
  106.             }
  107.     }
  108.     else {
  109.         /*  move == -1
  110.             new section for Adds Viewpoint 3a+ only
  111.             (no down scroll in hardware)        */
  112.         pfirst= cline;
  113.     /*    pfirst = loc(pfirst,(topline-1));*/
  114.         plast = loc(pfirst,SHEIGHT-topline);
  115.         adputpage(pfirst,topline);
  116.         }
  117.     putstatusline(cline);
  118. }
  119.  
  120. adputpage(tline,beg)            /* for Adds only: putpage from beg */
  121. int tline;
  122. sint beg;
  123. {
  124.     int y,line;
  125.     char c;
  126.     
  127.     for (line=tline, y=beg; line <= plast; line++, y++) {
  128.         if (cline == line) {
  129.             cursory = y;
  130.             cursorx = adjustc(cursorx);
  131.             }
  132.         putline(line,  y);
  133.           /* check for repeated scrolls */
  134.         if (inbufp && (cline != 1) && (cline != lastl) )
  135.             if ((c=inbuf[0]==tran[SCRLUPKEY]) ||
  136.                 (c==tran[SCRLDNKEY]) ) return;
  137.         }
  138.     if (y <= SHEIGHT) delpage(y);
  139. }
  140.  
  141. calcjmp()          /* calc number of lines to jump */
  142. {
  143. int to;
  144.     jmpto = 1;              /* reset jmpto for repeat incrmtl jumps*/
  145.     if (ans[0] == '+')
  146.         if ((to=atoi(ans+1))) jumpline(to);
  147.     if (ans[0] == '-')
  148.         if ((to=atoi(ans))) jumpline((to));
  149.     if (ans[0] >= '0') {
  150.         if ((to=atoi(ans))) jumpline(to-cline);
  151.         jmpto = to;
  152.         }
  153. }
  154.  
  155. jumpline(move)    /*move current line by move lines down, checking for
  156.             interrupt from user (if interrupted, do nothing,
  157.             and return NO) */
  158. int move;
  159. {
  160.     int line, dest;
  161.  
  162.     puttext();
  163.     dest=cline+move;
  164.     dest-=dest%100;
  165.     if (dest > lastread) {
  166.         gotoxy(WAITPOS,0);
  167.         line=cline;
  168.         while (line < dest && loc(line,100) != line) {
  169.             line+=100;
  170.             if (testkey() == ESCKEY) {
  171.                 error("Interrupted");
  172.                 return NO;
  173.                 }
  174.             }
  175.         }
  176.     moveline(move);
  177.     return YES;
  178. }
  179.  
  180. movepage(dir)    /*move current line by a page down (dir==0) or up (-1)*/
  181. int dir;
  182. {
  183.     int move, line;
  184.  
  185.     puttext();
  186.     move=(SHEIGHT-topline)/2 - PAGEOVERLAP;
  187.     if (dir) move=pfirst-cline-move;
  188.     else move=plast-cline+move;
  189.     if ( (cline=loc((line=cline),move) ) != line) {
  190.         gettext(cline);  
  191.         putpage();
  192.         }
  193. }
  194.  
  195. moveword(move)    /*move 1 word to the right (move -ve: left)*/
  196. int move;
  197. {
  198.     if (charn+move < 0) {
  199.         moveline(-1); 
  200.         charn=strlen(text);
  201.         goto cratend;        /*leave cursor at end of line */
  202.         }
  203.     else if (charn+move >= strlen(text)) {
  204.         moveline(1); 
  205.         sync(0);
  206.         if (inword(text[0])) return;
  207.         }
  208.     /* ORIG: while (move<0 && text[charn] && inword(text[charn])
  209.         && !inword(text[--charn]) && (charn+=move));*/
  210.         /* move off of 1st char of current word */
  211.     if (move<0) charn--;
  212.         /* span all chars in word */
  213.     while (move>=0 && text[charn] && inword(text[charn]) && (charn+=move));
  214.         /* ORIG: while ((move<0 || text[charn]) */
  215.         /* span nonchars */
  216.     while (( (move<0 && charn) || move >= 0) && text[charn]
  217.          && !inword(text[charn]) && (charn+=move));
  218.     if (move < 0) {
  219.         if (charn) {    /* span all chars in word */
  220.             while(inword(text[charn]) && --charn);
  221.                 { if (charn || !inword(text[charn]) ) charn++;  }
  222.             }
  223.         else    /* charn==0: go to end of previous line*/
  224.     if (!inword(text[0])) {
  225.             moveline(-1);
  226.             charn=strlen(text);
  227.             }
  228.     }
  229. cratend:
  230.     sync(charn);
  231. }
  232.  
  233.  
  234. #if (WWRAP)        /*do word wrap on char insert*/
  235. insertchar(c)    /*inserts 'c' at charn, moves cursor up  one */
  236. char c;
  237. {
  238.     int cp;
  239.     /*char *t;*/
  240.     int diffn,ncharn;
  241.     char *txtp,*posp;
  242.     char *firstwhite();
  243.     
  244.     if ( (cp=strlen(text)+1) >= (LLIM-1))
  245.         error("Line too long");
  246.     else {
  247.         for (; cp >= charn; cp--) text[cp+1]=text[cp];
  248.         text[charn]=c;
  249.         altered=YES;
  250.         rewrite(charn,cursorx);
  251.         sync(charn+1);
  252.             /*word wrap*/
  253.         if  ((cursorx>rtmarg) && c!=' ' && c!='\t') {
  254.             txtp=posp=&text[charn];
  255.             /*if ((ncharn=firstwhite(text,cursorx,charn)) == 0) ncharn=charn;*/
  256.             if ((posp=firstwhite(text,cursorx,txtp)) == text) ncharn=charn;
  257.             else ncharn=posp-text;
  258.             diffn=charn-ncharn;
  259.             sync(ncharn);
  260.             resetcursor();
  261.             crinsert(0);
  262.             sync(charn+diffn);
  263.             resetcursor();
  264.             }
  265.         }
  266. }
  267.  
  268. char *firstwhite(s,cp,tp)    /*find first prev. white*/
  269. char s[];                    /*string to search*/
  270. int cp;                        /*cursor pos and string (text) pos */
  271. char *tp;
  272. {
  273.     /*int cpos,pos;*/
  274.     int cpos;
  275.     char *posp,*lastnwht;
  276.     char c;
  277.  
  278.     lastnwht=tp;
  279.     posp=tp-1;
  280.     while( ( ((c=*posp)!=' ' && c!='\t') || cp>rtmarg) && posp>=s)
  281.     {
  282.         switch(c) {
  283.         case '\t' : 
  284.             if (!((cp-1)%tabwidth)) posp--;
  285.             break;
  286.         case ' '  :
  287.             posp--;
  288.             break;
  289.         default   :
  290.             lastnwht=(posp--);
  291.             break;
  292.         }
  293.         cp--;
  294.     }
  295.     return (lastnwht);
  296. }
  297.  
  298. #else        /*no wwrap*/
  299.  
  300. insertchar(c)    /*inserts 'c' at charn, moves cursor up  one */
  301. char c;
  302. {
  303.         int cx,cp,temp;
  304.     char *t;
  305.  
  306.     if ( (cp=strlen(text)+1) >= (LLIM-1))
  307.         error("Line too long");
  308.     else {
  309.         for (; cp >= charn; cp--) text[cp+1]=text[cp];
  310.         text[charn]=c;
  311.         altered=YES;
  312.         rewrite(charn,cursorx);
  313.         sync(charn+1);
  314.         }
  315. }
  316. #endif
  317.  
  318. deletechar(dir)    /*deletes char before (dir=-1) or at (dir=0) cursor */
  319. int dir;
  320. {
  321.     char c;
  322.     int cp;
  323.  
  324.     cp=charn+dir;
  325.     if (cp < 0) {
  326.         if (cline > 1) crdelete(-1);
  327.         }
  328.     else if (text[cp] == '\0') {
  329.         if (cline < lastl) crdelete(0);
  330.         }
  331.     else {
  332.         do { 
  333.             c=text[cp]=text[cp+1]; 
  334.             cp++; 
  335.             } 
  336.         while(c);
  337.         altered=YES;
  338.         sync(charn+dir);
  339.         if (calcoffset() != lastoff)rewrite(0,0);
  340.         else rewrite(charn,cursorx);
  341.         }
  342. }
  343.  
  344. crdelete(dir)    /*delete a [CR] before (dir==-1)or at (dir==0) cursor */
  345. int dir;
  346. {
  347.     int delline, len, *t;
  348.     char textb[LLIM];
  349.  
  350.     altered=YES;
  351.     if (dir == 0) {
  352.         delline=cline+1;
  353.         strcpy(textb,getline(delline));
  354.         cursory++;
  355.         }
  356.     else {
  357.         delline=cline;
  358.         strcpy(textb,text);
  359.         if (cline > 1) gettext(cline-1);
  360.         else puttext();
  361.         }
  362.     sync((len=strlen(text)));
  363.     if (len+strlen(textb) >= LLIM) {
  364.         textb[LLIM-len]='\0';
  365.         error("Line too long - cut short");
  366.         }
  367.     strcat(text,textb);
  368.     deltp(delline,1);
  369.     if (delline > plast || delline <= pfirst) {
  370.         puttext(); 
  371.         putpage();
  372.         }
  373.     else {
  374.         delpage(--cursory);
  375.         rewrite(0,0);
  376.         adputpage(delline,cu